#include "logicteam.h"
#include"networkmanager.h"
#include"messageconst.h"

DEFINE_SINGLE_ATTRIBUTES(LogicTeam);

LogicTeam::LogicTeam()
{
    NET_INSTANCE()->add(MESSAGE_TEAM_APPLY,handler(this,SEL_EVENTFUNC(LogicTeam::c2sTeamApply)));
    NET_INSTANCE()->add(MESSAGE_AGREE_TEAM,handler(this,SEL_EVENTFUNC(LogicTeam::c2sAgreeTeam)));
    NET_INSTANCE()->add(MESSAGE_DISSOLVE_TEAM,handler(this,SEL_EVENTFUNC(LogicTeam::c2sDissolveTeam)));
    NET_INSTANCE()->add(MESSAGE_REFUSE_TEAM,handler(this,SEL_EVENTFUNC(LogicTeam::c2sRefuseTeam)));
    NET_INSTANCE()->add(MESSAGE_TEAM_FIGHT,handler(this,SEL_EVENTFUNC(LogicTeam::c2sTeamFight)));
    NET_INSTANCE()->add(MESSAGE_TEAM_GOTO_MAP,handler(this,SEL_EVENTFUNC(LogicTeam::c2sTeamGotoMap)));
    NET_INSTANCE()->add(MESSAGE_TEAM_MOVE,handler(this,SEL_EVENTFUNC(LogicTeam::c2sTeamMove)));
}

void LogicTeam::c2sTeamApply(Json::Value& msg)
{
    int fd=NET_INSTANCE()->getFd();
    auto& playermaps=NET_SOCKET().m_playermaps;
    msg["fd"]=fd;
    int dest=msg["dest"].asInt();
    if(playermaps.find(dest)!=playermaps.end())
    {
        cout<<playermaps[fd].rolename<<" apply to make a team with "<<playermaps[dest].rolename<<endl;
        NET_INSTANCE()->send(dest,MESSAGE_TEAM_APPLY,msg);
        //SendMsg(msg->dest,(char*)msg,sizeof(TeamApply_Msg));
    }
}

void LogicTeam::c2sRefuseTeam(Json::Value& msg)
{
    int fd=NET_INSTANCE()->getFd();
    auto& playermaps=NET_SOCKET().m_playermaps;
    msg["fd"]=fd;
    int dest=msg["dest"].asInt();
    if(playermaps.find(dest)!=playermaps.end())
    {
        cout<<playermaps[fd].rolename<<" refused "<<playermaps[dest].rolename<<" team apply"<<endl;
        NET_INSTANCE()->send(dest,MESSAGE_REFUSE_TEAM,msg);
        //SendMsg(msg->dest,(char*)msg,sizeof(RefuseTeam_Msg));
    }
}

void LogicTeam::c2sAgreeTeam(Json::Value& msg)
{
    int fd=NET_INSTANCE()->getFd();
    auto& playermaps=NET_SOCKET().m_playermaps;
    msg["fd"]=fd;
    int dest=msg["dest"].asInt();
    if(playermaps.find(dest)!=playermaps.end())
    {
        cout<<playermaps[fd].rolename<<" agree "<<playermaps[dest].rolename<<" team apply"<<endl;
        NET_INSTANCE()->send(dest,MESSAGE_AGREE_TEAM,msg);
        // SendMsg(msg->dest,(char*)msg,sizeof(AgreeTeam_Msg));
    }
}

void LogicTeam::c2sTeamMove(Json::Value& msg)
{
    int fd=NET_INSTANCE()->getFd();
    auto& playermaps=NET_SOCKET().m_playermaps;
    msg["fd"]=fd;
    int dest=msg["dest"].asInt();
    if(playermaps.find(dest)!=playermaps.end())
    {
        cout<<playermaps[fd].rolename<<" team header move "<<playermaps[dest].rolename<<" to ("<<msg["x"].asFloat()<<","<<msg["y"].asFloat()<<")"<<endl;
        NET_INSTANCE()->send(dest,MESSAGE_TEAM_MOVE,msg);
        //SendMsg(msg->dest,(char*)msg,sizeof(TeamMove_Msg));
    }
}

void LogicTeam::c2sTeamGotoMap(Json::Value& msg)
{
    int fd=NET_INSTANCE()->getFd();
    auto& playermaps=NET_SOCKET().m_playermaps;
    int dest=msg["dest"].asInt();
    if(playermaps.find(dest)!=playermaps.end())
    {
        cout<<playermaps[fd].rolename<<" team header move "<<playermaps[dest].rolename<<" to "<<msg["map"].asString()<<endl;
        NET_INSTANCE()->send(dest,MESSAGE_TEAM_GOTO_MAP,msg);
        //SendMsg(msg->dest,(char*)msg,sizeof(TeamGotoMap_Msg));
    }
}

void LogicTeam::c2sDissolveTeam(Json::Value& msg)
{
    int fd=NET_INSTANCE()->getFd();
    auto& playermaps=NET_SOCKET().m_playermaps;
    msg["fd"]=fd;
    int dest=msg["dest"].asInt();
    if(playermaps.find(dest)!=playermaps.end())
    {
        cout<<"Dissolve Team "<<playermaps[fd].rolename<<" and "<<playermaps[dest].rolename<<endl;
        NET_INSTANCE()->send(dest,MESSAGE_DISSOLVE_TEAM,msg);
        //SendMsg(msg->dest,(char*)msg,sizeof(TeamManage_Msg));
    }
}

 void LogicTeam::c2sTeamFight(Json::Value& msg)
 {
     int fd=NET_INSTANCE()->getFd();
     auto& playermaps=NET_SOCKET().m_playermaps;
     msg["fd"]=fd;
     int dest=msg["dest"].asInt();
     if(playermaps.find(dest)!=playermaps.end())
     {
         cout<<playermaps[fd].rolename<<" leader "<<playermaps[dest].rolename<<" entry fight"<<endl;
         NET_INSTANCE()->send(dest,MESSAGE_TEAM_FIGHT,msg);
         //SendMsg(msg->dest,(char*)msg,sizeof(TeamFight_Msg));
     }
 }
